package com.blb.ssh.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.blb.ssh.dao.RoleDao;
import com.blb.ssh.entity.Menu;
import com.blb.ssh.entity.Role;
import com.blb.ssh.service.RoleService;

public class RoleServiceImpl implements RoleService{

	private RoleDao roleDao ;
	
	
	public RoleDao getRoleDao() {
		return roleDao;
	}
	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}
	//查询数据总条数
	@Override
	public Long getRoleCount() {
		return roleDao.getRoleCount();
	}
	//查询所有数据
	@Override
	public List<Role> getRoleListByMap(int page, int rows) {
		return roleDao.getRoleListByMap(page,rows);
	}
	//条件查询
	public List<Role> getRoleListByMap(String rolename, int page, int rows){
		return roleDao.getRoleListByMap(rolename,page,rows);
	}
	//删除
	public boolean batchDeleteRole(String ids) {
		String[]  idstr=StringUtils.split(ids, ",");
		boolean isok=true;
		//先通过roleId删除用户的数据
		boolean isko1=roleDao.batchDeleteRole(idstr);
		//再通过roleId删除t_role_menu中相应的数据
		boolean isko2=roleDao.deleteRoleMenu(idstr);
		if(isko1==true && isko2==true){
			isok=true;
		}else{
			isok=false;
		}
		return	isok;
	}
	//新增
	@Override
	public boolean addRole(Role role) {
		Role r=roleDao.getUserByUserName(role.getRoleName());
		if(r !=null){
			boolean isok=false;
			return isok;
		}
		return roleDao.addRole(role);
	}
	//修改
	@Override
	public boolean editRole(Role role) {
		return roleDao.editRole(role);
	}
	//获取roleid对应的全部menu信息
	public List<Menu> getmenuId(int roleId){
		return roleDao.getmenuId(roleId);
	}
	//获取menu的全部
	public List<Menu> getAllTree(){
		return roleDao.getAllTree();
	}
	
	public boolean editRole(String ids, int roleId){
		String[]  idstr=StringUtils.split(ids, ",");
		boolean isok=true;
		//先通过roleId删除原来的数据
		boolean isko1=roleDao.deleteRoleMenu(roleId);
		//再通过新传过来的menuId重新添加
		boolean isko2=roleDao.addRoleMenu(idstr,roleId);
		if(isko1==true && isko2==true){
			isok=true;
		}else{
			isok=false;
		}
		return isok;
	}
}
